package com.lee.xcore.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction

/**启动Activity*/
inline fun <reified T : Activity> Context.onStart() {
    startActivity(Intent(this, T::class.java))
}

/**启动Activity*/
inline fun <reified T : Activity> Context.onStart(mKey: String, mBundle: Bundle?) {
    val mIntent = Intent(this, T::class.java)
    mIntent.putExtra(mKey, mBundle)
    startActivity(mIntent)
}

/**启动Activity*/
inline fun <reified T : Activity> Context.onStart(vararg pair: Pair<String, String>?) {
    val mIntent = Intent(this, T::class.java)
    pair?.let {
        pair.forEach {
            mIntent.putExtra(it!!.first, it!!.second)
        }
    }
    startActivity(mIntent)
}

/**启动Activity*/
inline fun <reified T : Activity> Context.onStart(mBundle: Bundle?) {
    val mIntent = Intent(this, T::class.java)
    mBundle?.let {
        mIntent.putExtras(it)
    }
    startActivity(mIntent)
}


/**启动Activity*/
inline fun <reified T : Activity> Activity.onStart() {
    startActivity(Intent(this, T::class.java))
}

/**启动Activity*/
inline fun <reified T : Activity> Activity.onStart(mKey: String, mBundle: Bundle?) {
    val mIntent = Intent(this, T::class.java)
    mIntent.putExtra(mKey, mBundle)
    startActivity(mIntent)
}

/**启动Activity*/
inline fun <reified T : Activity> Activity.onStart(mBundle: Bundle?) {
    val mIntent = Intent(this, T::class.java)
    mBundle?.let {
        mIntent.putExtras(it)
    }
    startActivity(mIntent)
}

/**启动Activity*/
inline fun <reified T : Activity> Activity.onStart(vararg pair: Pair<String, String>?) {
    val mIntent = Intent(this, T::class.java)
    pair?.let {
        pair.forEach {
            mIntent.putExtra(it!!.first, it!!.second)
        }
    }
    startActivity(mIntent)
}

/**启动Activity*/
inline fun <reified T : Activity> Activity.onStartResult(requestCode: Int) {
    startActivityForResult(Intent(this, T::class.java), requestCode, null)
}

/**启动Activity*/
inline fun <reified T : Activity> Activity.onStartResult(requestCode: Int, mBundle: Bundle?) {
    startActivityForResult(Intent(this, T::class.java), requestCode, mBundle)
}

/**启动Activity*/
inline fun <reified T : Activity> Activity.onStartResult(
    requestCode: Int,
    vararg pair: Pair<String, String>
) {
    val mIntent = Intent(this, T::class.java)
    val mBundle = Bundle()
    pair?.let {
        pair.forEach {
            mBundle.putString(it!!.first, it!!.second)
        }
    }
    startActivityForResult(mIntent, requestCode, mBundle)
}

/**
 * 跳转应用商城
 */
fun Activity.jumpMarket(packageNameStr: String?){
    val mAddress = "market://details?id=${if(packageNameStr.isNullOrEmpty())packageName else packageNameStr}"
    kotlin.runCatching {
        val marketIntent = Intent("android.intent.action.VIEW")
        marketIntent.data = Uri.parse(mAddress)
        startActivity(marketIntent)
    }
}

inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> FragmentTransaction) {
    beginTransaction().func().commit()
}
fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int){
    supportFragmentManager.inTransaction { add(frameId, fragment) }
}

fun AppCompatActivity.replaceFragment(fragment: Fragment, frameId: Int) {
    supportFragmentManager.inTransaction{replace(frameId, fragment)}
}
